<?php
/*
 +----------------------------------------------------------------------
 + Title        : Base 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2019/6/6 15:36
 + Last-time    : 2019/6/6 15:36+ 86187
 + Desc         : 接口管理  基类
 +----------------------------------------------------------------------
*/


namespace app\admin\controller;


use app\admin\service\NodeService;
use app\admin\service\TokenService;
use app\api\service\Token;
use app\common\lib\exception\ApiException;
use app\common\lib\exception\HttpResponseException;
use app\common\lib\exception\WebAdminApiException;
use library\Controller;
use think\facade\Request;

class Base extends Controller

{
    protected $middleware = ['CrossDomain'];
    protected $body;
    protected $userId = -1;

    protected $fitter = [
        '/admin/login/login',
        '/api/admin/sys'
    ];

	/**
	 * @Desc:
	 * @throws ApiException
	 * @throws \app\http\exception\ApiException
	 *
	 *
	 * @User: Randy_chen
	 * @Date: 2019/6/6
	 * @Time: 16:23
	 */
	public function __construct() {
		parent::__construct(); // TODO: Change the autogenerated stub
		$this->body = input();
		//token检测
		if (!in_array(Request::url(), $this->fitter)) {

			$res = TokenService::check(Request::header('Authorization'));
			if (!$res) {
				throw new ApiException(11003);
			} else {
				$user = json_decode(json_encode($res->user), true);
				$this->body['user'] = $user;
				$this->body['uid'] = $user['id'];
				$this->userId = $user['id'];
			}
			$this->auth();
		}
    }

    /**
     * @Desc:
     * @return bool
     * @throws ApiException
     *
     *
     * @User: Randy_chen
     * @Date: 2019/6/6
     * @Time: 16:22
     */
    public function auth()
    {
        try {
            NodeService::checkAuthByApi();
        } catch (\ReflectionException $e) {
            $this->error($e->getCode(),$e->getMessage());
        } catch (ApiException $e) {
            $this->error($e->getCode(),$e->getMessage());
        }
    }

    /**
     * @Desc:空操作
     * @param $name
     *
     * @User: Randy_chen
     * @Date: 2019/6/6
     * @Time: 17:23
     */
    public function _empty($name)
    {
        $this->error(10102);
    }

    /**
     * 返回失败的操作
     * @param mixed $msg 消息内容
     * @param array $data 返回数据
     * @param integer $code 返回代码
     */
    public function error($code = 1,$msg='', $data = [] ){
        !$code && $code=1;
        $msg = $msg ? : (ApiException::ERR_MAP[$code] ? : 'FAIL.');
        $result = ['code' => $code, 'msg' => $msg, 'data' => $data];
        throw new WebAdminApiException(json($result));
    }

    /**
     * 返回成功的操作
     * @param mixed $msg 消息内容
     * @param array $data 返回数据
     * @param integer $code 返回代码
     */
    public function success( $data = [],$msg='', $code = 0)
    {
        $msg = $msg ? : (ApiException::ERR_MAP[$code] ? : 'SUCCESS');
        $result = ['code' => $code, 'msg' => $msg, 'data' => $data];
        throw new WebAdminApiException(json($result));
    }


    /**
     * 失败返回
     * @param string $msg
     * @param int    $code
     * @param array  $data
     *
     * @author : Randy_chen
     * @Date   : 2019/9/24
     * @Time   : 14:10
     */
    public function returnError($code = 1, $msg = '',$data=[]){
        $msg = $msg ? : (ApiException::ERR_MAP[$code] ? : 'FAIL.');
        $result = ['code' => $code, 'msg' => $msg, 'data' => $data];
        throw new WebAdminApiException(json($result));
    }

    /**
     * 返回成功的操作
     * @param mixed $msg 消息内容
     * @param array $data 返回数据
     * @param integer $code 返回代码
     */
    public function returnSuccess($data = [], $msg = '', $code = 0 , $ur='' , $header=[])
    {
        $msg = $msg ? : (ApiException::ERR_MAP[$code] ? : 'SUCCESS');
        $result = ['code' => $code, 'msg' => $msg, 'data' => $data];
        throw new WebAdminApiException(json($result));
    }
}